<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ESP32 音频播放器 - 配置</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary: #4361ee;
            --secondary: #3a0ca3;
            --success: #4cc9f0;
            --danger: #f72585;
            --warning: #fca311;
            --light: #f8f9fa;
            --dark: #212529;
            --gray: #6c757d;
            --card-bg: #ffffff;
            --body-bg: #f0f2f5;
            --border-radius: 12px;
            --box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: var(--body-bg);
            color: var(--dark);
            line-height: 1.6;
            padding: 20px 15px;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .container {
            width: 100%;
            max-width: 600px;
            margin: 0 auto;
        }

        header {
            text-align: center;
            margin-bottom: 30px;
            width: 100%;
        }

        h1 {
            color: var(--primary);
            font-size: 2.2rem;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 15px;
        }

        h2 {
            color: var(--secondary);
            font-size: 1.5rem;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .card {
            background: var(--card-bg);
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            padding: 25px;
            margin-bottom: 25px;
            transition: var(--transition);
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.12);
        }

        .slider-container {
            margin: 20px 0;
        }

        .slider {
            -webkit-appearance: none;
            width: 100%;
            height: 10px;
            border-radius: 5px;
            background: #e9ecef;
            outline: none;
        }

        .slider::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 22px;
            height: 22px;
            border-radius: 50%;
            background: var(--primary);
            cursor: pointer;
            transition: var(--transition);
        }

        .slider::-webkit-slider-thumb:hover {
            background: var(--secondary);
            transform: scale(1.1);
        }

        .value-display {
            font-weight: 600;
            color: var(--primary);
            font-size: 1.2rem;
            margin-left: 5px;
        }

        .control-group {
            display: flex;
            gap: 15px;
            margin-top: 20px;
            flex-wrap: wrap;
        }

        .control-group .btn {
            flex: 1;
            min-width: 140px;
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: var(--border-radius);
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .btn-primary {
            background: var(--primary);
            color: white;
        }

        .btn-primary:hover {
            background: #3651d4;
            transform: translateY(-2px);
        }

        .btn-success {
            background: var(--success);
            color: white;
        }

        .btn-success:hover {
            background: #3ab7d9;
            transform: translateY(-2px);
        }

        .select-group {
            margin-bottom: 20px;
        }

        select {
            width: 100%;
            padding: 12px 16px;
            border: 1px solid #ddd;
            border-radius: var(--border-radius);
            font-size: 16px;
            background: white;
            transition: var(--transition);
        }

        select:focus {
            border-color: var(--primary);
            outline: none;
            box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.2);
        }

        footer {
            margin-top: 30px;
            text-align: center;
            color: var(--gray);
            font-size: 0.9rem;
        }

        @media (max-width: 576px) {
            h1 {
                font-size: 1.8rem;
            }
            .card {
                padding: 20px;
            }
            .control-group {
                flex-direction: column;
            }
            .control-group .btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1><i class="fas fa-cogs"></i> 系统配置</h1>
            <p>设置ESP32音频播放器的默认参数</p>
        </header>

        <!-- 定时关机 -->
        <div class="card">
            <h2><i class="fas fa-clock"></i> 定时关机</h2>
            <div class="select-group">
                <label for="shutdownTimer">选择关机倒计时：</label>
                <select id="shutdownTimer">
                    <option value="0" selected>不启用定时关机</option>
                    <option value="10">10分钟后自动关机</option>
                    <option value="30">30分钟后自动关机</option>
                    <option value="60">60分钟后自动关机</option>
                    <option value="90">90分钟后自动关机</option>
                    <option value="180">2小时后自动关机</option>
                </select>
            </div>
            <button class="btn btn-danger" onclick="setShutdownTimer()">
                <i class="fas fa-hourglass-end"></i> 设置定时关机
            </button>
        </div>

        <!-- 开机音量设置 -->
        <div class="card">
            <h2><i class="fas fa-volume-up"></i> 开机默认音量</h2>
            <div class="slider-container">
                <p>音量: <span class="value-display" id="volDisplay">12</span>/21</p>
                <input type="range" class="slider" id="defaultVolume" min="0" max="21" value="12" oninput="updateVol(this.value)">
            </div>
            <button class="btn btn-primary" onclick="setDefaultVolume()">
                <i class="fas fa-save"></i> 保存默认音量
            </button>
        </div>

        <!-- 开机亮度设置 -->
        <div class="card">
            <h2><i class="fas fa-lightbulb"></i> 开机默认亮度</h2>
            <div class="slider-container">
                <p>亮度: <span class="value-display" id="brightDisplay">50</span>%</p>
                <input type="range" class="slider" id="defaultBrightness" min="0" max="100" value="50" oninput="updateBright(this.value)">
            </div>
            <button class="btn btn-primary" onclick="setDefaultBrightness()">
                <i class="fas fa-save"></i> 保存默认亮度
            </button>
        </div>

        <!-- 播放源配置 -->
        <div class="card">
            <h2><i class="fas fa-link"></i> 播放源配置</h2>
            <p>设置电台列表的 JSON 数据源地址：</p>
            <input 
                type="text" 
                id="jsonUrlInput" 
                class="url-input" 
                placeholder="请输入JSON数据源URL"
                value="https://fm-73y.pages.dev/list.json"
                style="width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: var(--border-radius); margin: 10px 0; font-size: 16px;">
            
            <div class="control-group">
                <button class="btn btn-primary" onclick="saveJsonUrl()">
                    <i class="fas fa-save"></i> 保存地址
                </button>
                <button class="btn btn-success" onclick="testJsonUrl()">
                    <i class="fas fa-sync-alt"></i> 测试连接
                </button>
            </div>
        </div>

        <footer>
            <p>ESP32音频播放器配置界面 &copy; by简 2025</p>
        </footer>
    </div>

    <script>
        function updateVol(val) {
            document.getElementById('volDisplay').innerText = val;
        }

        function updateBright(val) {
            document.getElementById('brightDisplay').innerText = val;
        }

        // 设置定时关机
        function setShutdownTimer() {
            const seconds = document.getElementById('shutdownTimer').value;
            if (seconds == 0) {
                alert('已取消定时关机');
                fetch('/canceloff', { method: 'GET' });
                return;
            }

            fetch('/shutdownoff', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'minutes=' + seconds
            })
            .then(r => r.text())
            .then(t => {
                const mins = seconds0;
                alert(`已设置 ${mins} 分钟后自动停止播放`);
            })
            .catch(err => {
                alert('设置失败: ' + err.message);
            });
        }

        // 保存默认音量
        function setDefaultVolume() {
            const vol = document.getElementById('defaultVolume').value;
            fetch('/setDefaultVolume', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'vol=' + vol
            })
            .then(r => r.text())
            .then(t => {
                alert('开机默认音量已保存为: ' + vol);
            })
            .catch(err => {
                alert('保存失败: ' + err.message);
            });
        }

        // 保存默认亮度
        function setDefaultBrightness() {
            const bright = document.getElementById('defaultBrightness').value;
            fetch('/setDefaultBrightness', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'brightness=' + bright
            })
            .then(r => r.text())
            .then(t => {
                alert('开机默认亮度已保存为: ' + bright + '%');
            })
            .catch(err => {
                alert('保存失败: ' + err.message);
            });
        }
        // 保存播放源JSON地址
        function saveJsonUrl() {
            const url = document.getElementById('jsonUrlInput').value.trim();
            if (!url) {
                alert('请输入有效的JSON地址');
                return;
            }

            fetch('/setJsonUrl', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'url=' + encodeURIComponent(url)
            })
            .then(r => r.text())
            .then(t => {
                alert('播放源地址已保存！\n\n' + t);
            })
            .catch(err => {
                alert('保存失败: ' + err.message);
            });
        }

        // 测试JSON地址是否有效
        function testJsonUrl() {
            const url = document.getElementById('jsonUrlInput').value.trim();
            if (!url) {
                alert('请输入要测试的JSON地址');
                return;
            }

            fetch(url)
                .then(r => {
                    if (!r.ok) throw new Error(`HTTP ${r.status}`);
                    return r.json();
                })
                .then(data => {
                    const count = Array.isArray(data) ? data.length : Object.keys(data).length;
                    alert(`✅ 连接成功！\n解析到 ${count} 个电台或条目。`);
                })
                .catch(err => {
                    alert(`❌ 测试失败：\n${err.message}\n\n请检查URL或网络格式是否为合法JSON。`);
                });
        }

        // 页面加载时尝试获取当前设置（可选）
        window.addEventListener('load', () => {
            fetch('/getJsonUrl')
            .then(r => r.text())
            .then(url => {
                document.getElementById('jsonUrlInput').value = url.trim();
            })
            .catch(err => {
                console.error('获取JSON地址失败:', err);
            });
            //获取亮度
            fetch('/getDefaultBrightness')
            .then(r => r.text())
            .then(defaultBrightness => {
                document.getElementById('defaultBrightness').value = defaultBrightness;
                updateBright(defaultBrightness)
            })
            .catch(err => {
                console.error('获取默认亮度失败:', err);
            });
            //获取声音
            fetch('/getDefaultVolume')
            .then(r => r.text())
            .then(volume => {
                document.getElementById('defaultVolume').value = volume;
                updateVol(volume);
            })
            .catch(err => {
                console.error('获取默认声音失败:', err);
            });



        });
    </script>
</body>
</html>